package de.sono.radio.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.Toast;
import de.sono.radio.R;
import de.sono.radio.fragments.RadioStreamer;
import de.sono.radio.fragments.RadioStreamer.OnRadioStationSelectListener;
import de.sono.radio.list.RadioListAdresses;
import de.sono.radio.list.RadioStation;
import de.sono.radio.service.RadioStreamingService;

public class RadioTab extends FragmentActivity implements
		OnRadioStationSelectListener {

	private String LOG_TAG = RadioTab.class.getSimpleName();

	private TabHost mTabHost;
	private TabManager mTabManager;
	private ViewPager mViewPager;

	public void onCreate(Bundle savedInstanceState) {

		Log.d(LOG_TAG, "onCreate()");

		super.onCreate(savedInstanceState);
		setContentView(R.layout.radiostreamer_tabs);

		mTabHost = (TabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup();

		mViewPager = (ViewPager) findViewById(R.id.tabcontent_pager);

		mTabManager = new TabManager(this, mTabHost, mViewPager);
		
		Bundle b = new Bundle();

		b.putString(RadioStreamer.RADIO_STATION_URL, RadioListAdresses.ARD_ALL);
		b.putInt(RadioStreamer.RADIO_LOADER_ID, 1);
		mTabManager.addTab(mTabHost.newTabSpec("alle").setIndicator("All"),
				RadioStreamer.class, b);

		b = new Bundle();
		b.putString(RadioStreamer.RADIO_STATION_URL, RadioListAdresses.ARD_POP);
		b.putInt(RadioStreamer.RADIO_LOADER_ID, 2);
		mTabManager.addTab(mTabHost.newTabSpec("pop").setIndicator("Pop"),
				RadioStreamer.class, b);

		b = new Bundle();
		b.putString(RadioStreamer.RADIO_STATION_URL,
				RadioListAdresses.ARD_REGIONAL);
		b.putInt(RadioStreamer.RADIO_LOADER_ID, 3);
		mTabManager.addTab(
				mTabHost.newTabSpec("regional").setIndicator("Regional"),
				RadioStreamer.class, b);

		b = new Bundle();
		b.putString(RadioStreamer.RADIO_STATION_URL, RadioListAdresses.ARD_INFO);
		b.putInt(RadioStreamer.RADIO_LOADER_ID, 4);
		mTabManager.addTab(mTabHost.newTabSpec("info").setIndicator("Info"),
				RadioStreamer.class, b);

		b = new Bundle();
		b.putString(RadioStreamer.RADIO_STATION_URL,
				RadioListAdresses.ARD_Kultur);
		b.putInt(RadioStreamer.RADIO_LOADER_ID, 5);
		mTabManager.addTab(
				mTabHost.newTabSpec("kultur").setIndicator("Kultur"),
				RadioStreamer.class, b);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.fragment_menu, menu);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.menu_refresh:
			RadioStreamer frag = (RadioStreamer) mTabManager.getItem(mTabHost
					.getCurrentTab());
			frag.reloadData();
			// getLoaderManager().initLoader(loaderId, getArguments(), this);
			break;
		case R.id.menu_help:
			Toast.makeText(this, "Help is on it's way...", Toast.LENGTH_LONG)
					.show();
		default:
		}

		return true;
	}

	@Override
	public void startStreamingService(RadioStation pStation) {
		Intent intent = new Intent(this, RadioStreamingService.class);
		intent.putExtra(RadioStreamingService.STREAM_START, true);
		intent.putExtra(RadioStreamingService.STREAM_URL,
				pStation.getStreamUrl());
		intent.putExtra(RadioStreamingService.STREAM_TITLE, pStation.getName());
		intent.putExtra(RadioStreamingService.STREAM_ICON, pStation.getImg());

		// startService(intent);

		Toast.makeText(this, pStation.getName(),
				Toast.LENGTH_LONG).show();

	}
}